SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[FN_COMPRAS_IMPORTE] (@ID AS NUMERIC(10,0)) RETURNS NUMERIC(10,2)
AS
BEGIN

DECLARE @TOTAL AS NUMERIC(10,2)= 0
DECLARE @CANTIDAD AS NUMERIC(10,2)
DECLARE @COSTO AS NUMERIC(10, 2)
DECLARE @DESCUENTO AS NUMERIC(10,2)
DECLARE @IMPUESTO AS NUMERIC(10,2)

			
DECLARE CUR CURSOR FOR SELECT COSTO, CANTIDAD, DESCUENTO, IMPUESTO FROM COMPRAS_DETALLES WHERE ID = @ID

OPEN CUR

FETCH NEXT FROM CUR INTO @COSTO, @CANTIDAD, @DESCUENTO, @IMPUESTO

WHILE @@FETCH_STATUS = 0
BEGIN

SET @TOTAL =  @TOTAL + ((@CANTIDAD * @COSTO) - @DESCUENTO) + @IMPUESTO

FETCH NEXT FROM CUR INTO @COSTO, @CANTIDAD, @DESCUENTO, @IMPUESTO
END
	CLOSE CUR
	DEALLOCATE CUR

RETURN ISNULL(@TOTAL,0)
END
GO
